﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;
using RR;

public partial class Default2 : System.Web.UI.Page
{
    readonly RegistRar regrar = new RegistRar();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void grd_funds_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        if (e.NewValues["CODE"] == null)
            e.NewValues["CODE"] = regrar.Gen_id("VZOBOROT_gen");
    }
    protected void grd_funds_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "DOG")
        {
            var combo = (ASPxComboBox)e.Editor;
            combo.Callback += new CallbackEventHandlerBase(cbbDog_OnCallback);

            var grd = e.Column.Grid;
            if (!combo.IsCallback)
            {
                string kontr = "-1";
                if (!grd.IsNewRowEditing)
                    kontr = Convert.ToString(grd.GetRowValues(e.VisibleIndex, "KONTRAGENT"));
                FillDogCombo(combo, kontr);
            }
        }
    }

    private void cbbDog_OnCallback(object sender, CallbackEventArgsBase e)
    {
        FillDogCombo(sender as ASPxComboBox, e.Parameter);
    }

    private void FillDogCombo(ASPxComboBox combo, string kontr)
    {
        var k = -1;
        Int32.TryParse(kontr, out k);
        if (k < 0) { combo.Items.Clear(); return; }

        ds_DOG.Where = "it.KONTRAGENT = @kontr";
        ds_DOG.WhereParameters.Clear();
        ds_DOG.WhereParameters.Add("kontr", DbType.Int32, kontr);
        combo.DataBind();
    }
}